﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MyFound
{
    public class FoundApi
    {
        public static decimal GetGrEquity(string code)
        {
            decimal val = 0;
            if (string.IsNullOrWhiteSpace(code))
                return val;
            try
            {
                //http://api.money.126.net/data/feed/164304,001158,001417,690009,money.api?callback=cb
                WebClient webClient = new WebClient();
                var html = webClient.DownloadString(string.Format("http://fund.eastmoney.com/{0}.html", code));
                var match = Regex.Match(html, @"id=""gz_gsz"">(?<val>[^<]+?)</span>");
                if (match.Success)
                {
                    decimal.TryParse(match.Groups["val"].Value, out val);
                }
            }
            catch
            {

            }

            return val;
        }

        public static Dictionary<string, Class1> ListFound(IEnumerable<string> codes)
        {
            Dictionary<string, Class1> dictionary = new Dictionary<string, Class1>();
            if (codes == null || !codes.Any())
                return dictionary;

            try
            {
                //http://api.money.126.net/data/feed/164304,001158,001417,690009,money.api?callback=cb
                WebClient webClient = new WebClient();
                var html = webClient.DownloadString(string.Format("http://api.money.126.net/data/feed/{0},money.api?callback=cb", string.Join(",", codes)));
                if (html.StartsWith("cb("))
                {
                    var json = html.Substring(3, html.LastIndexOf(")") - 3);
                    dictionary = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, Class1>>(json);
                  
                }
            }
            catch
            {

            }

            return dictionary;
        }

        public static Dictionary<string, decimal> ListGr()
        {
            Dictionary<string, decimal> dic=new Dictionary<string, decimal>();
                 try
            {
                WebClient webClient = new WebClient();
                var html = webClient.DownloadString("http://fund.eastmoney.com/fundguzhi.html");
                if (!string.IsNullOrWhiteSpace(html))
                {
                    var matches = Regex.Matches(html,
                        @"<input type=""checkbox"" id=""(?<id>.+?)""/>[\s\s]+?<td class=""gr"">(?<gr>.+?)</td>");
                    foreach (Match match in matches)
                    {
                        decimal val = 0;
                        if (decimal.TryParse(match.Groups["gr"].Value, out val))
                        {
                            dic[match.Groups["id"].Value] = val;
                        }
                        
                    }
                }
            }
            catch
            {

            }
            return dic;
        }
    }
}
